const http = require('http');

const testUrls = [
    '/api/v1/index/search',
    '/api/v1/index/category', 
    '/api/v1/index/hotpic',
    '/api/v1/index/rank',
    '/api/v1/index/latest',
    '/api/v1/index/latest_comment',
    '/api/v1/index/attention'
];

async function testInterface(path) {
    return new Promise((resolve, reject) => {
        const options = {
            hostname: 'localhost',
            port: 8000,
            path: path,
            method: 'GET'
        };

        const req = http.request(options, (res) => {
            let data = '';
            res.on('data', (chunk) => {
                data += chunk;
            });
            res.on('end', () => {
                try {
                    const jsonData = JSON.parse(data);
                    resolve({ path, status: res.statusCode, data: jsonData });
                } catch (e) {
                    resolve({ path, status: res.statusCode, data: data });
                }
            });
        });

        req.on('error', (e) => {
            reject({ path, error: e.message });
        });

        req.setTimeout(5000, () => {
            req.destroy();
            reject({ path, error: 'Timeout' });
        });

        req.end();
    });
}

async function runTests() {
    console.log('开始测试所有前端接口...\n');
    
    for (const url of testUrls) {
        try {
            const result = await testInterface(url);
            console.log(`✓ ${result.path}: ${result.status}`);
            if (result.data && typeof result.data === 'object') {
                console.log(`  Data fields: ${Object.keys(result.data).join(', ')}`);
                if (result.data.data && Array.isArray(result.data.data)) {
                    console.log(`  Data count: ${result.data.data.length}`);
                }
            }
        } catch (error) {
            console.log(`✗ ${error.path}: ${error.error}`);
        }
        console.log('');
    }
}

runTests().catch(console.error);
